package com.jiang.mall.mallproduct.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiang.common.utils.PageUtils;
import com.jiang.common.utils.Query;
import com.jiang.mall.mallproduct.product.dao.AttrGroupDao;
import com.jiang.mall.mallproduct.product.entity.AttrEntity;
import com.jiang.mall.mallproduct.product.entity.AttrGroupEntity;
import com.jiang.mall.mallproduct.product.service.AttrGroupService;
import com.jiang.mall.mallproduct.product.service.AttrService;
import com.jiang.mall.mallproduct.product.vo.AttrGroupWithAttrsVo;
import com.jiang.mall.mallproduct.product.vo.SpuItemAttrGroupVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        String key = (String) params.get("key");
        // 非模糊查询
        if (StringUtils.isEmpty(key)) {
            // 查询某一个具体的属性信息
            if (catelogId > 0) {
                QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId);
                IPage<AttrGroupEntity> page = this.page(new Query().getPage(params), wrapper);
                return new PageUtils(page);
            } else {
                // 查询所有属性信息
                IPage<AttrGroupEntity> page = this.page(new Query().getPage(params), new QueryWrapper<AttrGroupEntity>());
                return new PageUtils(page);
            }
        } else {
            // 模糊查询
            QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>().eq("1", "1");
            if (!StringUtils.isEmpty(key)) {
                wrapper.and((obj) -> {
                    obj.eq("attr_group_id", key).or().like("attr_group_name", key);
                });
            }
            IPage<AttrGroupEntity> page = this.page(new Query().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类Id查出所有分组以及这些分组里面的Id
     *
     * @param catId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> getGroupsWithAttrs(Long catId) {
        List<AttrGroupEntity> attrGroupEntities = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catId));
        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            List<AttrEntity> attrEntities = attrService.getAttrRelation(item.getAttrGroupId());
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            attrGroupWithAttrsVo.setAttrs(attrEntities);
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> spuItemAttrGroupVos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId, catalogId);

        return spuItemAttrGroupVos;
    }
}